define(function () {
    'use strict';

    function ctrl($scope, $stateParams, LocalCacheService, WebService, $state, TdxLocalFuncService, $ionicHistory, CommonService) {
        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });

        function init() {
            var user = LocalCacheService.getUser();
            $scope.riskInfo = {
                risk_level_name: user.risk_level_name,
                corp_risk_level: user.corp_risk_level,//5激进型 4积极型 3稳健型 2保守型 0未测评
                corp_risk_status: user.corp_risk_status,
                risk_end_date: user.corp_end_date
            };
            $scope.from = $stateParams["from"];
            showLevelDetail(user);
        }

        function showLevelDetail(user) {
            //FXJGJS 中台风险等级字典查询
            var params = {dict_entry: 'FXJGJS', subentry: user.corp_risk_level};
            WebService.qryDictInfo(params).then(
                function (data) {
                    $scope.riskInfo.msg = "--";
                    if (data && data.length > 0) {
                        $scope.riskInfo.msg = data[0].dict_prompt;
                    }
                }
            );
        }

        $scope.toRiskEval = function () {
            if (TdxLocalFuncService.isTDX()) {
                TdxLocalFuncService.tdxTradeLocked().then(function (data) {
                    if (data.text == '1' || data.text == 1) {
                        return;
                    } else {
                        if ($stateParams["from"]) {
                            $state.go("tab.account-riskEval", {
                                from: $stateParams["from"],
                                fromParams: $stateParams["fromParams"],
                                typeid: 1
                            });
                        } else {
                            $state.go("tab.account-riskEval");
                        }
                    }
                })
            } else {
                if ($stateParams["from"]) {
                    $state.go("tab.account-riskEval", {
                        from: $stateParams["from"],
                        fromParams: $stateParams["fromParams"],
                        typeid: 1
                    });
                } else {
                    $state.go("tab.account-riskEval");
                }
            }
        };
        $scope.goBuy = function () {
            if (TdxLocalFuncService.isTDX()) {
                TdxLocalFuncService.tdxTradeLocked().then(function (data) {
                    if (data.text == '1' || data.text == 1) {
                        return;
                    } else {
                        if ($stateParams["from"] && $stateParams["from"] == "trade-buy") {
                            $state.go($stateParams["from"], angular.fromJson($stateParams["fromParams"]));
                        }
                    }
                })
            } else {
                if ($stateParams["from"] && $stateParams["from"] == "trade-buy") {
                    $state.go($stateParams["from"], angular.fromJson($stateParams["fromParams"]));
                }
            }
        };
        $scope.next = function () {
            CommonService.showConfirm({
                btn1Text: '不同意',
                btn2Text: '同意',
                message: '已知晓本人的风险承受能力与本人所选择的投资产品不匹配，本人愿意承担由此带来的风险及造成的后果！'
            }, function () {
                return false;
            }, function () {
                $state.go('tab.account-OpenStocksHK', {typeid: $stateParams["fromParams"], go: 'must'})
            });
        };

        $scope.onBackKeyDown = function () {
            if ($scope.from == 'tab.account-OpenStocksHK') {
                $state.go('tab.account-index');
            } else {
                $ionicHistory.goBack();
            }
        }
    }

    ctrl.$inject = ['$scope', '$stateParams', 'LocalCacheService', 'WebService', '$state', 'TdxLocalFuncService', '$ionicHistory', 'CommonService'];
    return ctrl;
});